package com.heima.wemedia.controller.v1;


import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.constants.RedisConstants;
import com.heima.common.constants.WmNewsConstants;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.WmSensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.utils.common.SensitiveWordUtil;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.IWmSensitiveService;
import jdk.nashorn.internal.runtime.regexp.joni.ast.StringNode;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 敏感词信息表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-07-04
 */
@RestController
@RequestMapping("/wm-sensitive")
@AllArgsConstructor
@Slf4j
public class WmSensitiveController {

    private final IWmSensitiveService wmSensitiveService;
    private final RedisTemplate redisTemplate;
    private final WmSensitiveMapper  wmSensitiveMapper;


    @DeleteMapping("/del/{id}")
    public ResponseResult delete(@PathVariable("id") Integer id){
        wmSensitiveService.removeById(id);
        updateRedis();
        return ResponseResult.okResult("删除成功");
    }

    @PostMapping("/list")
    public PageResponseResult list(@RequestBody WmSensitiveDto dto){
        dto.checkParam();
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<WmSensitive>().like(dto.getSensitives() != null,WmSensitive::getSensitives,dto.getSensitives());
        Page<WmSensitive> page = new Page<>(dto.getPage(), dto.getSize());
        wmSensitiveService.page(page,wrapper);
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getRecords());
        return pageResponseResult;
    }

    @PostMapping("/save")
    public ResponseResult save(@RequestBody WmSensitiveDto dto){

        WmSensitive wmSensitive = new WmSensitive();
        BeanUtils.copyProperties(dto,wmSensitive);
        wmSensitive.setCreatedTime(new Date());
        wmSensitiveService.save(wmSensitive);
        updateRedis();
        return ResponseResult.okResult("");
    }

    @PostMapping("/update")
    public ResponseResult update(@RequestBody WmSensitiveDto dto){
        WmSensitive wmSensitive = new WmSensitive();
        BeanUtils.copyProperties(dto,wmSensitive);
        wmSensitiveService.updateById(wmSensitive);
        updateRedis();
        return ResponseResult.okResult("");
    }

    @PostMapping("/saveBatch")
    public ResponseResult saveBatch(@RequestBody List<WmSensitive> reqCustomerDailyImports){
        wmSensitiveService.saveBatch(reqCustomerDailyImports);
        return ResponseResult.okResult(null);
    }

    @GetMapping("/findAll")
    public List<WmSensitive> findAll()throws IOException {
       return wmSensitiveService.list(null);
    }

    private void updateRedis() {
        redisTemplate.delete(WmNewsConstants.SENSITIVE_KEY);
        // 执行缓存预热业务...
        List<WmSensitive> wmSensitives = wmSensitiveMapper.selectList(null);
        List<String> collect = wmSensitives.stream().map(WmSensitive::getSensitives).collect(Collectors.toList());
        //初始化敏感词
        SensitiveWordUtil.initMap(collect);
        redisTemplate.opsForValue().set(WmNewsConstants.SENSITIVE_KEY, JSONObject.toJSONString(collect));
    }

}
